package com.town.userdemo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.town.userdemo.common.CommonCode;
import com.town.userdemo.common.CommonPageParam;
import com.town.userdemo.common.CommonResponse;
import com.town.userdemo.common.CommonResult;
import com.town.userdemo.entity.User;
import com.town.userdemo.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author macro
 * @since 2021-04-08
 */
@RestController
@RequestMapping("/user")
public class UserController {

        @Autowired
        private UserService userService;
// put delete post put
//    增删改查
        @ApiOperation("添加用户")
        @PutMapping("/add")
        public CommonResult add(@RequestBody User user){
            boolean save = userService.save(user);
            if (save) {
                return CommonResponse.makeRsp(CommonCode.SUCCESS, user.getId().toString());
            }
            return CommonResponse.makeRsp(CommonCode.FAIL);
        }

        @ApiOperation("修改用户信息")
        @PutMapping("/update")
        public CommonResult update(@RequestBody User user) {
            boolean update = userService.updateById(user);
            if (update) {
                return CommonResponse.makeRsp(CommonCode.SUCCESS);
            }
            return CommonResponse.makeRsp(CommonCode.FAIL);
        }

        @ApiOperation("删除用户")
        @DeleteMapping("/delete")
        public CommonResult delete(@RequestParam Long id) {
            boolean delete = userService.removeById(id);
            if (delete) {
                return CommonResponse.makeRsp(CommonCode.SUCCESS);
            }
            return CommonResponse.makeRsp(CommonCode.FAIL);
        }


        @ApiOperation("查询用户信息")
        @PostMapping("/select")
        public CommonResult select(@RequestBody CommonPageParam<User> commonPageParam) {
            QueryWrapper<User> queryWrapper = new QueryWrapper<>(commonPageParam.getData());
            IPage page = userService.page(commonPageParam.getPage(), queryWrapper);
            return CommonResponse.makePageRsp(CommonCode.SUCCESS, page);
        }

    }





